home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11474 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  56 lines

  1. Path: solon.com!not-for-mail
  2. From: brianmcg@interaccess.com (Brian V. McGroarty)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: Multiple Indirection (Pointer Arithmatic)
  5. Date: 24 Mar 1996 11:40:34 -0600
  6. Organization: InterAccess, Chicago's best Internet Service Provider
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4j41ei$nfa@solutions.solon.com>
  10. References: <4j06ig$7q8@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12. X-Newsreader: News Xpress Version 1.0 Beta #4
  13.  
  14.  
  15. R Ghosh-Roy wrote:
  16. >I have three arrays (A,B,C) of structures and each structure has its own
  17. >set
  18. >of fields. In structure A, one particular field Ab points to structure B. B
  19. >has a field Bc which points to C and C has a field named Cd. I need to look
  20. >for Cd for each A. Can I use some kind of clever pointer arithmatic to get
  21. >to Cd for each A? Or, is there a cleverer way?
  22.  
  23.  
  24. >Note: 3 arrays of structures A,B and C and only one field relates elements
  25. >      of each array, ie, Ab relates to an element within an array of Bs and
  26. >      Bc relates to an element within an array of Cs. These are dynamically
  27. >      assigned.
  28.  
  29.  
  30. [...]
  31.  
  32. >I was thinking of using sizeof and offsetof expressions. 
  33.  
  34.  
  35. There's no need for anything too strange.  Since the member of A which points 
  36. to B should be of type "struct B *" the compiler already knows what you will 
  37. be pointing to.  Consider something like the following:
  38.  
  39. struct C *newCPointer;
  40. newCPointer= structSetA[ 5 ].Ab->Bc;
  41.  
  42.  
  43. To avoid obfuscated code, you could use the following.  Any good compiler 
  44. should generate the same code for the above as for the below:
  45.  
  46. struct B *newBPointer;
  47. struct C *newCPointer;
  48. newBPointer= structSetA[ 5 ].Ab;
  49. newCPointer= newBPointer->Bc;
  50.  
  51.  
  52.  
  53. ---
  54. Brian Valters McGroarty -- brianmcg@bix.com
  55. phone/fax (847) 439-7714
  56.